using System.Collections.Generic;
using FairyGUI;
using UnityEngine;
using DG.Tweening;

public class RenderWindowMediator : MyWindow {
    RenderImage _renderImage;
    FixedRoom _fixedRoom;
    List<GameObject> _blocks;
    List<GameObject> _walls;
    List<GameObject> _cubes;
    List<GameObject> _lights;
    public RenderWindowMediator()
    {
        Object block = Resources.Load("Map/RoomBlock");
        Object wall = Resources.Load("Map/RoomWall");
        Object cube = Resources.Load("Map/RoomCube");
        GameObjectPoolMgr.InitPool("RoomBlock", (GameObject)Object.Instantiate(block));
        GameObjectPoolMgr.InitPool("RoomWall", (GameObject)Object.Instantiate(wall));
        GameObjectPoolMgr.InitPool("RoomCube", (GameObject)Object.Instantiate(cube));
        GameObjectPoolMgr.InitPool("RoomLight", CreateLight());
        _blocks = new List<GameObject>();
        _walls = new List<GameObject>();
        _cubes = new List<GameObject>();
        _lights = new List<GameObject>();
    }

    GameObject CreateLight(){
        Transform light = new GameObject("light").transform;
        Light lightCom = light.gameObject.AddComponent<Light>();
        lightCom.type = LightType.Point;
        lightCom.shadows = LightShadows.Hard;
        return light.gameObject;
        // light.SetParent(wall.transform, false);
        // light.localPosition = new Vector3(0, 2, 0);
    }
    protected override void OnInit()
    {
        this.contentPane = UIPackage.CreateObject("MapPackages", "RenderWindow").asCom;
        // this.SetXY(200, 50);
        GGraph holder = contentPane.GetChild("renderTexture").asGraph;
        _renderImage = new RenderImage(holder);
        //RenderImage是不透明的，可以设置最多两张图片作为背景图
        _renderImage.SetBackground(contentPane.GetChild("backGround"));

        SwipeGesture gesture1 = new SwipeGesture(contentPane.GetChild("holder"));
        gesture1.actionDistance = 50;
        gesture1.onMove.Add(OnSwipeMove);
        gesture1.onEnd.Add(OnSwipeEnd);

        contentPane.GetChild("resetBtn").asButton.onClick.Add(() => {
            _renderImage.ResetPos();
        });
    }

    override protected void OnShown()
    {
        _renderImage.ResetPos();
        // _renderImage.LoadModel("Role/npc");
        _renderImage.modelRoot.localPosition = new Vector3(-2.7f, -0.5f, 11f);
        // _renderImage.modelRoot.localScale = new Vector3(1, 1, 1);
        // _renderImage.modelRoot.localRotation = Quaternion.Euler(0, 120, 0);
    }

    public void InitData(FixedRoom room){
        if (room == null) return;
        _fixedRoom = room;
        UnloadModel();

        int index = 0;
        for (int y = _fixedRoom.height - 1; y >= 0 ; y--)
        {
            for (int x = 0; x < _fixedRoom.width; x++)
            {
                if (_fixedRoom.roomArrangeList[index] == GMConfig.RoomArrange.Wall || _fixedRoom.roomArrangeList[index] == GMConfig.RoomArrange.Light) {
                    GameObject wall = GameObjectPoolMgr.GetObject("RoomWall", new Vector3(x * 2, 0, y * 2));
                    _walls.Add(wall);
                    //添加光源
                    if (_fixedRoom.roomArrangeList[index] == GMConfig.RoomArrange.Light){
                        GameObject light = GameObjectPoolMgr.GetObject("RoomLight");
                        light.transform.SetParent(wall.transform);
                        light.transform.localPosition = new Vector3(0, 2, 0);
                        _lights.Add(light);

                    }
                }
                else{
                    _blocks.Add(GameObjectPoolMgr.GetObject("RoomBlock", new Vector3(x * 2, 0, y * 2)));
                    if (_fixedRoom.roomArrangeList[index] != GMConfig.RoomArrange.None)
                        _cubes.Add(GameObjectPoolMgr.GetObject("RoomCube", new Vector3(x * 2,1,y * 2)));
                }
                index++;
            }
        }
        if (_renderImage != null)
            _renderImage.ReceiveMap(_blocks,_walls,_cubes);

    }

    void UnloadModel(){
        if (_blocks.Count > 0)
        {
            foreach (GameObject obj in _blocks)
            {
                GameObjectPoolMgr.PutObject("RoomBlock",obj);
            }
        }

        if (_walls.Count > 0)
        {
            foreach (GameObject obj in _walls)
            {
                GameObjectPoolMgr.PutObject("RoomWall", obj);
            }
        }

        if (_cubes.Count > 0)
        {
            foreach (GameObject obj in _cubes)
            {
                GameObjectPoolMgr.PutObject("RoomCube", obj);
            }
        }

        if (_lights.Count > 0)
        {
            foreach (GameObject obj in _lights)
            {
                GameObjectPoolMgr.PutObject("RoomLight", obj);
            }
        }
        _walls.Clear();
        _blocks.Clear();
        _cubes.Clear();
        _lights.Clear();
    }

    void OnSwipeMove(EventContext context)
    {
        Debug.Log(context.ToString());
        SwipeGesture gesture = (SwipeGesture)context.sender;
        Vector3 v = new Vector3();
        if (Mathf.Abs(gesture.delta.x) > Mathf.Abs(gesture.delta.y))
        {
            v.x = -Mathf.Round(gesture.delta.x) * 0.3f;
            // if (Mathf.Abs(v.x) < 2) //消除手抖的影响
            //     return;
        }
        else
        {
            v.z = Mathf.Round(gesture.delta.y) * 0.3f;
            // if (Mathf.Abs(v.z) < 2)
            //     return;
        }
        // _ball.Rotate(v, Space.World);
        _renderImage._camera.transform.Translate(v,Space.World);
    }

    void OnSwipeEnd(EventContext context)
    {
        SwipeGesture gesture = (SwipeGesture)context.sender;
        Vector3 v = new Vector3();
        if (Mathf.Abs(gesture.velocity.x) > Mathf.Abs(gesture.velocity.y))
        {
            v.x = -Mathf.Round(Mathf.Sign(gesture.velocity.x));
            if (Mathf.Abs(v.x) < 2)
                return;
        }
        else
        {
            v.z = Mathf.Round(Mathf.Sign(gesture.velocity.y));
            if (Mathf.Abs(v.z) < 2)
                return;
        }
        // _ball.DORotate(v, 0.3f, RotateMode.WorldAxisAdd);
        _renderImage._camera.transform.DOMove(v, 0.3f, true);

    }

    void GenerateMap(){

        // GameObject go = ((GameObject)Object.Instantiate(prefab));
        // _model = go.transform;
        // _model.SetParent(this.modelRoot, false);


    }
}